package com.wei.consumer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.wei.entity.User;
import com.wei.remote.UserControllerRemoteClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/api/user")
public class UserController {
    /**
     * springcloud-provice 指的是提供者 spring.application.name
     */
    private final static String REQUESTION_URL = "http://springcloud-provice/api/user/getOne";
    /**
     * 调用http的方式
     * 1. HttpClient(apache)
     * 2. HttpUrlConnection(JDK)
     * 3. RestTemplate(Spring + Ribbon进行封装)
     * 4. okHttp
     * 5. Feign(netflix)
     */
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserControllerRemoteClient userControllerRemoteClient;

    @GetMapping("/getOne")
    public User getOne(){
        ResponseEntity<User> forEntity = restTemplate.getForEntity(REQUESTION_URL, User.class);
        return forEntity.getBody();
    }

    @GetMapping("/getOne1")
    public User getOne1(){
        return userControllerRemoteClient.getOne();
    }

    /**
     * 不会降级
     * @return
     */
    @GetMapping("/getOne2")
    @HystrixCommand(fallbackMethod = "fallback")
    public User getOne2(){
        return userControllerRemoteClient.getOne();
    }

    /**
     * 默认时间是1秒 等待1秒后 就会降级
     * @return
     */
    @GetMapping("/getOne3")
    @HystrixCommand(fallbackMethod = "fallback"
            // 目前在配置文件里面进行配置了
//            , commandProperties = {
//            @HystrixProperty(name="execution.timeout.enabled", value = "true"),
//            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value = "5000")}
    )
    public User getOne3(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return userControllerRemoteClient.getOne();
    }

    /**
     * 测试 配置文件中配置了超时时间
     * @return
     */
    @GetMapping("/getOne4")
    @HystrixCommand(fallbackMethod = "fallback")
    public User getOne4(){
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return userControllerRemoteClient.getOne();
    }

    /**
     * 测试抛出异常
     * @return
     */
    @GetMapping("/getOne5")
    @HystrixCommand(fallbackMethod = "fallback")
    public User getOne5(){
        String result = null;
        if( result==null){
            throw  new RuntimeException("抛出异常");
        }
        return userControllerRemoteClient.getOne();
    }

    /**
     * Hystrix限流: 基于线程和信号量进行限流
     * @return
     */
    @GetMapping("/getOne6")
    @HystrixCommand(fallbackMethod = "fallback",
    threadPoolKey = "user",
    threadPoolProperties = {
            @HystrixProperty(name="coreSize", value = "2"), //当前线程数
            @HystrixProperty(name="maxQueueSize", value = "1")//队列中的线程数
    })
    public User getOne6(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return userControllerRemoteClient.getOne();
    }

    /**
     * 在common中进行feign和hystrix的整合
     * @return
     */
    @GetMapping("/getOne7")
    public User getOne7(){
        return userControllerRemoteClient.getOne();
    }

    /**
     * 获取配置文件的信息
     * @return
     */
    @GetMapping("/getValue")
    public String getValue(){
        return userControllerRemoteClient.getValue();
    }

    /**
     * 降级方法
     * @return
     */
    public User fallback(Throwable throwable){
        throwable.printStackTrace();
        User user = new User();
        user.setUsername("demo");
        user.setPassword("服务降级了");
        return user;
    }
}
